/*
 *
0x5601021f5eb0 0x5601021f5eb0 0x5601021f5eb0
111
111
111

 */

#include <iostream>
using namespace std;

class Singleton{
private:
	int item;
	static int m_count; //记录引用次数
	static Singleton* m_instance;
	//构造函数私有
	Singleton(int item = 0){
		this->item = item;
	}
	//拷贝构造函数私有(无函数内容)：如果不重新定义且放在private修饰符下，则代码可能会通过默认公有的拷贝构造函数构造新的Singleton对象。
	Singleton(const Singleton& that){}
public:
	static Singleton& getInstance(){
		if(m_instance == NULL){
			m_instance = new Singleton(111);
		}
		m_count++;
		return *m_instance;
	}
	void print(){
		cout << item << endl;
	}
	//释放堆内空间
	void release(){
		m_count--;
		if(m_count == 0){
			delete m_instance;
			m_instance = NULL;
		}
	}
	~Singleton(){
		cout << "destroy Singleton" << endl;
	}
};
Singleton* Singleton::m_instance = NULL;
int Singleton::m_count = 0;

int main(void){

	//singleton_lazy.cpp:40:19: error: ‘Singleton::Singleton(int)’ is private within this context
    // 40 |         Singleton s1;
	//Singleton s1;  //error
	Singleton& s2 = Singleton::getInstance();
	Singleton& s3 = Singleton::getInstance();
	Singleton& s4 = Singleton::getInstance();

	//观察是不是同一个对象
	cout << &s2 << " " << &s3 << " " << &s4 << endl;

	s2.print();
	s3.print();
	s4.print();

	return 0;
}
